package com.jxfgyj.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jxfgyj.domain.DO.jxfgyj_teacher;
import com.jxfgyj.service.TeacherService;
import com.opensymphony.xwork2.ActionSupport;

public class TeacherAction extends ActionSupport implements ServletResponseAware, ServletRequestAware {

	private HttpServletResponse http_response;
	private HttpServletRequest http_request;
	/*
	 * 
	 */
	private TeacherService teacherService;
	/*
	 * 
	 */
	private jxfgyj_teacher teacher;
	private String search;

	/*
	 * 
	 */
	public String TeacherManage() {
		return "TeacherManage";
	}

	/*
	 * 
	 */
	public void createTeacher() {
		http_response.setContentType("text/html;charset=utf-8");
		try {
			if (teacherService.createTeacher(teacher)) {

				http_response.getWriter().write("1");

			} else {
				http_response.getWriter().write("-1");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void listTeacher() {
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.setPrettyPrinting();// 格式化json数据
		Gson gson = gsonBuilder.serializeNulls().create();
		//
		http_response.setContentType("text/html;charset=utf-8");
		List<jxfgyj_teacher> listTeacher = teacherService.listTeacher(search);
		try {
			http_response.getWriter().write(gson.toJson(listTeacher));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void getTeacher_byID() {
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.setPrettyPrinting();// 格式化json数据
		Gson gson = gsonBuilder.serializeNulls().create();
		//
		http_response.setContentType("text/html;charset=utf-8");
		jxfgyj_teacher return_teacher = teacherService.getTeacher_byID(teacher);
		try {
			http_response.getWriter().write(gson.toJson(return_teacher));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void deleteTeacher() {
		teacherService.deleteTeacher(teacher.getTeacher_id());

		http_response.setContentType("text/html;charset=utf-8");
		try {
			http_response.getWriter().write("1");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/*
	 * 
	 */
	@Override
	public void setServletRequest(HttpServletRequest http_request) {
		this.http_request = http_request;
	}

	@Override
	public void setServletResponse(HttpServletResponse http_response) {
		this.http_response = http_response;

	}

	public HttpServletResponse getHttp_response() {
		return http_response;
	}

	public void setHttp_response(HttpServletResponse http_response) {
		this.http_response = http_response;
	}

	public HttpServletRequest getHttp_request() {
		return http_request;
	}

	public void setHttp_request(HttpServletRequest http_request) {
		this.http_request = http_request;
	}

	/*
	 * 
	 */
	public TeacherService getTeacherService() {
		return teacherService;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	/*
	 * 
	 */
	public jxfgyj_teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(jxfgyj_teacher teacher) {
		this.teacher = teacher;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	/*
	 * 
	 */

}
